;(function(w){
    w.damu={};//将damu绑给window,使用命名空间来解决命名冲突
    w.damu.css=function (node,type,val){
   if(typeof node==="object"&&typeof node["transform"]==="undefined"){
       node["transform"]={}
   }
   if(arguments.length>=3){
       var text="";
       node["transform"] [type]=val;
       for(item in  node["transform"]){
           if( node["transform"].hasOwnProperty(item)){//如果item是对象的直接属性
           switch (item) {
               case "translateX":
               case "translateY":
                   text +=item+"("+node["transform"] [item]+"px)"
                   break;
                   case "scale":
                   text +=item+"("+node["transform"] [item]+")"
                   break;
                   case "rotate":
                   text +=item+"("+node["transform"] [item]+"deg)"
                   break;
           }
           }
       }
           node.style.transform=node.style.webkitTransform=text;
      
   }else if(arguments.length===2){
       val=node["transform"][type];
       if(typeof val==="undefined"){
       switch (type) {
               case "translateX":
               case "translateY":
               case "translateZ":
               case "rotate":
                val=0;
               break;
               case "scale":
                val=1;
               break;
       }
       }
       return val;
   }
    }
   w.damu.vMove=function(wrap,callBack){//竖向滑屏组件
       var item=wrap.children[0];
       damu.css(item,"translateZ",0.1);
         var start={};//手指一开始的距离
         var element={};//元素一开始的位置
         //快速滑屏的参数
         var lastTime=0;//开始的时间
         var lastPoint=0;//开始的位置
         var timeDis=1;//防止进去不拖动直接点一下
         var pointDis=0;//手指touchmove的真正的有效距离
         var isY=true;
         var isFirst=true;
         var clearTime=0;
         var Tween={//即点即停
           Linear:function(t,b,c,d){return c*t/d+b;},
           back: function(t,b,c,d,s){
               if (s == undefined) s = 1.70158;
               return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
           }
       }
         var minY=wrap.clientHeight-item.offsetHeight;
         wrap.addEventListener("touchstart",function(ev){
              ev=ev||event;
             var touchC=event.changedTouches[0];//获取第一根手指的位置
             minY=wrap.clientHeight-item.offsetHeight;//重置
             start={clientX:touchC.clientX,clientY:touchC.clientY};//解决存值存址的bug
             element.y=damu.css(item,"translateY");
             element.x=damu.css(item,"translateX");
             item.style.transition="none";
             lastTime=new Date().getTime();
             lastPoint=touchC.clientY;
             pointDis=0;//清除速度的残留
             item.handMove=false;
             isY=true;
             isFirst=true;
             clearInterval(clearTime);//即点即停
             if(callBack&&typeof callBack["start"]==="function"){
                 callBack["start"].call(item);
             }
         })
         wrap.addEventListener("touchmove",function(ev){
             if(!isY){
                 return;
             }
              ev=ev||event;
             var touchC=event.changedTouches[0];//获取第一根手指的位置
           //   var nowY=touchC.clientY;
           //   var disY=nowY-startY;//整个move过程的实际距离
           //   var translateY=elementY+disY;
           var now=touchC;
           var dis={};
           dis.y=now.clientY-start.clientY;
           dis.x=now.clientX-start.clientX;
           var translateY=element.y+dis.y;
           if(isFirst){
               isFirst=false;
               if(Math.abs(dis.x)>Math.abs(dis.y)){
                   isY=false;
                   return;
               }
           }
   
             var nowTime=new Date().getTime();
              var nowPoint=touchC.clientY;
              timeDis=nowTime-lastTime;
              pointDis=nowPoint-lastPoint;
              lastPoint=nowPoint;//每次滑动的最后一个位置也是下一次滑动的第一个位置
              lastTime=nowTime;
             //手动滑屏橡皮筋效果
             if(translateY>0){
                 item.handMove=true;
                 // 如果不加2,scale的值在(0,1),加2,值在(0,.5)
                 var scale=document.documentElement.clientHeight/((document.documentElement.clientHeight+translateY)*1.5);
                 translateY=damu.css(item,"translateY")+pointDis*scale;
             }else if(translateY<minY){
                 item.handMove=true;
                 var over=minY-translateY;
                 var scale=document.documentElement.clientHeight/((document.documentElement.clientHeight+over)*1.5);
                 translateY=damu.css(item,"translateY")+pointDis*scale;
             
             }
             damu.css(item,"translateY",translateY);
             if(callBack&&typeof callBack["move"]==="function"){
               callBack["move"].call(item);
           }
         })
         wrap.addEventListener("touchend",function(ev){
             var translateY=damu.css(item,"translateY");
             if(!item.handMove){//快速滑屏
                 //速度越大,位移越远
             var speed=pointDis/timeDis;
             speed=Math.abs(speed)<0.3?0:speed;
             var targetY=translateY+speed*200;
             //  item.style.transition=".5s transform";
              
              var time=Math.abs(speed)*0.2;
              time=time<0.8?0.8:time;
           //    time=time>2?2:time;
           //    var bsr="";
              var type="Linear";
             if(targetY>0){
                 targetY=0;
                 type="back";
                 //bsr="cubic-bezier(.26,1.51,.68,1.54)";
                 // damu.css(item,"translateY",translateY);
                 //快速滑屏橡皮筋效果
             }else if(targetY<minY){
                 targetY=minY; 
                 type="back";
                 //bsr="cubic-bezier(.26,1.51,.68,1.54)";
             //    damu.css(item,"translateY",translateY);   
             }
             // item.style.transition=".5s transform";
           //    item.style.transition=time+"s " +bsr+ " transform";
           //   damu.css(item,"translateY",targetY);
              bsr(type,targetY,time);
             }else{//手动滑屏
                 var translateY=damu.css(item,"translateY");
                 item.style.transition=".5s transform"
                 if(translateY>0){
                 translateY=0;
                 damu.css(item,"translateY",translateY)
             }else if(translateY<minY){
                translateY=minY;
                 damu.css(item,"translateY",translateY)
             }
             if(callBack&&typeof callBack["end"]==="function"){
               callBack["end"].call(item);
           }
             } 
         })
         function bsr(type,targetY,time){
             clearInterval(clearTime);
             var t=0//当前次数
             var b=damu.css(item,"translateY");//初始位置
             var c=targetY-b;//最终位置-初始位置
             var d=time*1000/(1000/60);//总次数
           clearTime=setInterval(function(){
               t++;
               if(callBack&&typeof callBack["move"]==="function"){
                   callBack["move"].call(item);
               }
               if(t>d){
                   if(callBack&&typeof callBack["end"]==="function"){
                       callBack["end"].call(item);
                   }
                   clearInterval(clearTime);  
               }
               var point=Tween[type](t,b,c,d);
               damu.css(item,"translateY",point);
              
             },1000/60)
             
         }
     }
})(window)
